package com.kehongborui.fliggy.controller;

import com.kehongborui.fliggy.annotation.LogAction;
import com.kehongborui.fliggy.annotation.Permission;
import com.kehongborui.fliggy.entity.RoleModule;
import com.kehongborui.fliggy.entity.User;
import com.kehongborui.fliggy.enums.LOGIN_USER;
import com.kehongborui.fliggy.enums.Role;
import com.kehongborui.fliggy.model.LoginUser;
import com.kehongborui.fliggy.model.ResultModel;
import com.kehongborui.fliggy.service.LoginService;
import com.kehongborui.fliggy.service.ModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

import java.util.List;

import static com.kehongborui.fliggy.enums.LOGIN_USER.MODULE_LIST;
import static com.kehongborui.fliggy.enums.Role.ADMIN;
import static com.kehongborui.fliggy.enums.Role.MERCHANT;
import static com.kehongborui.fliggy.enums.Role.USER;
import static com.kehongborui.fliggy.util.CommonUtils.isEmpty;

/**
 * @author zhang peng yang ming
 * @CopyRight (C) http://www.csdn.com
 * @brief
 * @email 122162005@qq.com
 * @date 2018-08-29
 */
@Controller
public class LoginController {
    @Autowired
    private LoginService loginService;

    @Autowired
    private ModuleService moduleService;

    @LogAction(action = "获取登录页面")
    @GetMapping("/login")
    public String loginPage(HttpServletRequest req) {
        // 进入登陆页面，判断是否有重定向页面(获取需要登录才能访问的页面的路径，及即将访问但被拦截的路径)
        String redirectURL = req.getQueryString();
        if (isEmpty(redirectURL)) { // 如果为空则表示为从非必须拦截页面过来
            // 获取上一次访问的路径
            redirectURL = req.getHeader("Referer");
        }

        // 如果用户直接访问登录页面，则登录过后跳转到主页
        if (isEmpty(redirectURL)) {
            redirectURL = "http://localhost:8080/"; // "http://" + req.getServerName() + ":" + req.getServerPort()
        }

        req.setAttribute("redirectURL", redirectURL);
        return "login";
    }

    @LogAction(action = "提交登录数据")
    @PostMapping("/login")
    @ResponseBody
    public ResultModel login(LoginUser user, HttpServletRequest req) {
        user.setLastIp(req.getRemoteAddr());
        // 查询登录的用户信息
        User retUser = loginService.login(user);
        // "redirectURL=http://localhost:8080/search/vehicle?depStation=%E9%87%91%E5%A0%82%E7%AB%99&depDate=2018-08-24&arrStation=%E9%81%82%E5%AE%81%E7%AB%99"
        // 将用户信息放到session tocken(令牌)

        // 查询当前用户对应的模块
        List<RoleModule> moduleList = moduleService.findByRoleId(retUser.getRoleId());

        req.getSession().setAttribute(MODULE_LIST.name(), moduleList);
        req.getSession().setAttribute(LOGIN_USER.USER.name(), retUser);

        return ResultModel.OK(req.getQueryString().replaceAll("redirectURL=", ""));
    }

    @Permission(permits = {ADMIN, USER, MERCHANT})
    @LogAction(action = "退出登录")
    @GetMapping("/logout")
    public String logout(HttpServletRequest req) {
        req.getSession().invalidate();
        return "redirect:login";
    }
}
